package com.cloud.drore.eboss.supplier.mapper.provider;

import com.cloud.drore.eboos.common.util.DateToStringUtil;
import com.cloud.drore.eboss.supplier.entity.CombinationProductStock;
import org.apache.commons.lang.StringUtils;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/2/10 11:11 <br>
 * @Author: <a href="575842778@qq.com">郑永涛</a>
 */
public class CombinationProductStockProvider {

    public CombinationProductStockProvider(){

    }

    private static String selectComProStockOneTodaySql = "SELECT " +
            "  product.`id` AS common_product_id,product.product_num," +
            "  stock.*,proAsso.distributor_id" +
            " FROM eboos_product product " +
            "  INNER JOIN eboos_combination_product comPro " +
            "    ON product.product_id = comPro.id " +
            "  INNER JOIN eboos_combination_product_stock stock " +
            "    ON comPro.`id` = stock.`combination_product_id`" +
            "    INNER JOIN `eboos_distributor_product_association` proAsso" +
            "    ON product.id = proAsso.`main_id`" +
            "WHERE product.`product_type` = 'COMBINATION_PRODUCT' " +
            "  AND comPro.`is_deleted` = 0 " +
            "  AND stock.`is_deleted` = 0 " +
            "  AND NOW() > stock.start_time " +
            "  AND NOW() < stock.end_time ";

    private static String selectComProStockByParamSql = "select id,inventory,start_time,end_time,inventory_type" +
            " from eboos_combination_product_stock where is_deleted = '0' ";

    public String updateStockByParam(CombinationProductStock combinationProductStock){
        int count = 0;
        String sql = " update eboos_combination_product_stock set ";
        if (combinationProductStock.getInventory()!=null) {
            sql += " inventory =" + combinationProductStock.getInventory() + "";
            count++;
        }
        if (combinationProductStock.getStartTime()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " start_time='" + DateToStringUtil.getStringDateTime(combinationProductStock.getStartTime())  + "'";
            count++;
        }
        if (combinationProductStock.getEndTime()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " end_time ='" + DateToStringUtil.getStringDateTime(combinationProductStock.getEndTime()) + "'";
            count++;
        }
        if (combinationProductStock.getIsDeleted()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " is_deleted ='" + combinationProductStock.getIsDeleted() + "'";
            count++;
        }
        sql += " where id='" + combinationProductStock.getId() + "'";
        return sql;
    }

    public String selectComProStockOneToday(){
        return selectComProStockOneTodaySql ;
    }


    public String selectComProStockByParam(CombinationProductStock combinationProductStock){
        String sql = selectComProStockByParamSql;
        if(combinationProductStock.getCombinationProductId() != null)
            sql += " and combination_product_id = " + combinationProductStock.getCombinationProductId() + "";
        if(combinationProductStock.getInventory() != null)
            sql += " and inventory = " + combinationProductStock.getInventory() + "";
        if(StringUtils.isNotBlank(combinationProductStock.getInventoryType()))
            sql += " and inventory_type = " + combinationProductStock.getInventoryType() + "";
        return sql;
    }

}